/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.roque.rueda.photobucket.ui;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

import java.util.List;
import java.util.ArrayList;

import com.photobucket.api.service.PhotobucketService;
import com.photobucket.api.service.SearchStrategy;
import com.photobucket.api.service.model.Media;

/**
 * Web service that encapsulates operations from photobucket api
 * @author Roque Rueda
 */
@WebService(serviceName = "PhotobucketRoque")
public class PhotobucketRoque {

    private final static String DEVELOPER_KEY = "149832496";
    private final static String PRIVATE_KEY = "769c5ce8af99f16afe80c9e9caad2932";
    
    /** Web service that is use to search in photobucket */
    @WebMethod(operationName = "search")
    public SearchResult search(@WebParam(name = "searchTerm") String searchTerm, 
    @WebParam(name = "numOfResults") int numOfResults, @WebParam(name = "page") int page, 
    @WebParam(name = "perPage") int perPage, @WebParam(name = "mediaType") String mediaType) {
        PhotobucketService pbService = new PhotobucketService(DEVELOPER_KEY, PRIVATE_KEY);
        SearchStrategy strategy = new SearchStrategy();
        
        SearchResult result = new SearchResult();
        try{
            if(mediaType != null && mediaType.equalsIgnoreCase("image"))
            {
                strategy.setMediaType(SearchStrategy.MediaType.IMAGES);
            }
            else if(mediaType != null && mediaType.equalsIgnoreCase("video"))
            {
                strategy.setMediaType(SearchStrategy.MediaType.VIDEOS);
            }
            
            strategy.setTerm(searchTerm);
            strategy.setNumResults(numOfResults);
            strategy.setPage(page);
            strategy.setPerPage(perPage);
            strategy.setOffset(0);
            pbService.execute(strategy);
            //strategy.getTotalResults();
            //strategy.getTotalPages();
            //result = strategy.getPrimaryMedia();
            result = new SearchResult(strategy.getTotalResults(), strategy.getTotalPages(), strategy.getPrimaryMedia());
        }
        catch (Exception e)
        {
            
        }
        return result;
    }
}
